//package com.breakfast.fun.ui.user.address;
//
//import android.content.Intent;
//
//import com.breakfast.fun.Constant;
//import com.breakfast.fun.data.entity.home.CityData;
//import com.breakfast.fun.data.entity.user.address.Building;
//import com.breakfast.fun.data.entity.user.address.Road;
//import com.breakfast.fun.ui.other.city.CityActivity;
//import com.breakfast.fun.util.Api;
//import com.breakfast.fun.util.L;
//import com.breakfast.fun.util.SharedUtil;
//import com.breakfast.fun.util.T;
//
//import java.util.Map;
//
//import rx.Subscriber;
//import rx.android.schedulers.AndroidSchedulers;
//import rx.schedulers.Schedulers;
//
///**
// * Created by yukai on 16/9/15.
// */
//public class ChooseBuildingPresenter extends ChooseBuildingContract.Presenter {
//
//    @Override
//    public void getBuilding(int roadId) {
//        Map<String, String> map = Api.getInstance().getBaseParams();
//
//        map.put("action","list");
//        map.put("areaId", roadId+"");
//
//        showLoadingDialog(true);
//        rxManager.add(model.getBuildingModel(map)
//                .subscribeOn(Schedulers.computation())
//                .observeOn(AndroidSchedulers.mainThread())
//                .unsubscribeOn(Schedulers.io())
//                .subscribe(new Subscriber<Building>() {
//                    @Override
//                    public void onCompleted() {
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        hideLoadingDialog();
//                        T.show("获取数据失败");
//                        e.printStackTrace();
//                    }
//
//                    @Override
//                    public void onNext(Building building) {
//                        hideLoadingDialog();
//                        if (building.isSuccess()) {
//                            view.showBuilding(building);
//                        } else {
//                            T.show(building.getMsg());
//                        }
//                    }
//
//                }));
//    }
//
//    @Override
//    public void getRoad() {
//        Map<String, String> map = Api.getInstance().getBaseParams();
//
//        map.put("action","list");
//        if(Constant.getCityData() != null) {
//            Constant.setCityData(CityData.objectFromData(SharedUtil.getString(context,"city")));
//            if(Constant.getCityData()!=null) {
//                map.put("cityName", Constant.getCityData().getCityName());
//            }else{
//                T.show("请选择城市");
//                Intent intent = new Intent(context, CityActivity.class);
//                context.startActivity(intent);
//                return ;
//            }
//        }
//
//        showLoadingDialog(true);
//        rxManager.add(model.getRoadModel(map)
//                .subscribeOn(Schedulers.computation())
//                .observeOn(AndroidSchedulers.mainThread())
//                .unsubscribeOn(Schedulers.io())
//                .subscribe(new Subscriber<Road>() {
//                    @Override
//                    public void onCompleted() {
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        hideLoadingDialog();
//                        T.show("获取数据失败");
//                        L.e(e.toString());
//                    }
//
//                    @Override
//                    public void onNext(Road road) {
//                        hideLoadingDialog();
//                        if (road.isSuccess()) {
//                            view.showRoad(road);
//                        } else {
//                            T.show(road.getMsg());
//                        }
//                    }
//
//                }));
//    }
//
//    @Override
//    public void searchBuilding(String building,boolean isLocated) {
//        Map<String, String> map = Api.getInstance().getBaseParams();
//
//        map.put("action","search");
//        map.put("key", building);
//
//        showLoadingDialog(true);
//        rxManager.add(model.getSearchBuildingModel(map)
//                .subscribeOn(Schedulers.computation())
//                .observeOn(AndroidSchedulers.mainThread())
//                .unsubscribeOn(Schedulers.io())
//                .subscribe(new Subscriber<Building>() {
//                    @Override
//                    public void onCompleted() {
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        hideLoadingDialog();
//                        T.show("获取数据失败");
//                        L.e(e.toString());
//                    }
//
//
//                    @Override
//                    public void onNext(Building building) {
//                        hideLoadingDialog();
//                        if (building.isSuccess()) {
//                            if(building.getData().size() == 0){
//                                if(isLocated){          //定位完成后先搜索是否开通，改变adapter状态。
//                                    view.setAdapterStatus();
//                                }else {                 //点击搜索后
//                                    T.show("搜索结果为空");
//                                    view.dialToServer();
//                                }
//                                return ;
//                            }else{
//                                      //通过搜索用showBuilding设置adapter，定位后的building数据在定位代码后set Adapter
//                                    view.showBuilding(building);
//
//                            }
//                        } else {
//                            T.show(building.getMsg());
//                        }
//                    }
//
//                }));
//    }
//
//
//
//    @Override
//    public void onStart() {
////        getRoad();
//
//    }
//}
package com.breakfast.fun.ui.user.address;

import android.content.Intent;

import com.breakfast.fun.Constant;
import com.breakfast.fun.data.entity.home.CityData;
import com.breakfast.fun.data.entity.user.address.Building;
import com.breakfast.fun.data.entity.user.address.Road;
import com.breakfast.fun.ui.other.city.CityActivity;
import com.breakfast.fun.util.Api;
import com.breakfast.fun.util.L;
import com.breakfast.fun.util.SharedUtil;
import com.breakfast.fun.util.T;

import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yukai on 16/9/15.
 */
public class ChooseBuildingPresenter extends ChooseBuildingContract.Presenter {

    @Override
    public void getBuilding(int roadId) {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("action", "list");
        map.put("areaId", roadId + "");

        showLoadingDialog(true);
        rxManager.add(model.getBuildingModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Building>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoadingDialog();
                        T.show("获取数据失败");
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Building building) {
                        hideLoadingDialog();
                        if (building.isSuccess()) {
                            view.showBuilding(building);
                        } else {
                            T.show(building.getMsg());
                        }
                    }

                }));
    }

    @Override
    public void getRoad() {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("action", "list");
        if (Constant.getCityData() != null) {
            Constant.setCityData(CityData.objectFromData(SharedUtil.getString(context, "city")));
            if (Constant.getCityData() != null) {
                map.put("cityName", Constant.getCityData().getCityName());
            } else {
                T.show("请选择城市");
                Intent intent = new Intent(context, CityActivity.class);
                context.startActivity(intent);
                return;
            }
        }

        showLoadingDialog(true);
        rxManager.add(model.getRoadModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Road>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoadingDialog();
                        T.show("获取数据失败");
                        L.e(e.toString());
                    }

                    @Override
                    public void onNext(Road road) {
                        hideLoadingDialog();
                        if (road.isSuccess()) {
                            view.showRoad(road);
                        } else {
                            T.show(road.getMsg());
                        }
                    }

                }));
    }

    @Override
    public void searchBuilding(String building) {
        Map<String, String> map = Api.getInstance().getBaseParams();

        map.put("action", "search");
        map.put("key", building);

        showLoadingDialog(true);
        rxManager.add(model.getSearchBuildingModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Building>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoadingDialog();
                        T.show("获取数据失败");
                        L.e(e.toString());
                    }

                    @Override
                    public void onNext(Building building) {
                        hideLoadingDialog();
                        if (building.isSuccess()) {
                            if (building.getData().size() == 0) {
                                T.show("搜索结果为空");
                                view.showBuilding(null);
                                return;
                            }
                            view.showBuilding(building);
                        } else {
                            T.show(building.getMsg());
                        }
                    }

                }));
    }


    @Override
    public void onStart() {
        getRoad();
    }
}
